//
//  ZCXVedioDetialTableViewCell.m
//  LoveOfPet
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXVedioDetialTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
//#import <AVFoundation/AVFoundation.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXVedioDetialTableViewCell ()
@property (nonatomic, retain) UIImageView *userImageV;
@property (nonatomic, retain) UILabel *userLabel;
@property (nonatomic, retain) UILabel *playCount;
@property (nonatomic, retain) UILabel *likeCount;
@property (nonatomic, retain) UILabel *feelLabel;
@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, retain) UIView *containerView;
@property (nonatomic, retain) UIProgressView *progressView;
@property (nonatomic, assign) BOOL playState;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, copy) NSString *strID;
@property (nonatomic, copy) NSString *userName;
@end
@implementation ZCXVedioDetialTableViewCell
- (void)dealloc{
    [_userImageV release];
    [_userLabel release];
    [_playCount release];
    [_likeCount release];
    [_feelLabel release];
    [_player release];
    [_containerView release];
    [_progressView release];
    [_imageV release];
    [_strID release];
    [_userName release];
    [_vedioDetialModel release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.userImageV = [[UIImageView alloc]init];
        [self.contentView addSubview:_userImageV];
        _userImageV.userInteractionEnabled = YES;
        [_userImageV release];
        
        UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userImageViewTapAction:)];
        [_userImageV addGestureRecognizer:imageTap];
        [imageTap release];
        
        self.userLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_userLabel];
        [_userLabel release];
        
        self.playCount = [[UILabel alloc]init];
        [self.contentView addSubview:_playCount];
        [_playCount release];
        
        self.likeCount = [[UILabel alloc]init];
        [self.contentView addSubview:_likeCount];
        [_likeCount release];
        
        self.feelLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_feelLabel];
        [_feelLabel release];
        
        self.containerView = [[UIView alloc]init];
        [self.contentView addSubview:_containerView];
        [_containerView release];
        
        self.progressView = [[UIProgressView alloc]init];
        [self.contentView addSubview:_progressView];
        [_progressView release];
        
        self.imageV = [[UIImageView alloc] init];
        _imageV.image = [UIImage imageNamed:@"Zpause"];
        [self.contentView addSubview:_imageV];
        _imageV.tag = 1000;
        [_imageV release];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _userImageV.layer.masksToBounds = YES;
    
    _userImageV.backgroundColor = [UIColor grayColor];

    _userImageV.layer.cornerRadius = 25 * WIDTH;
    _userImageV.frame = CGRectMake(5 * WIDTH, 5 * HEIGHT, 50 * WIDTH   , 50 * HEIGHT);
    _userLabel.frame = CGRectMake(_userImageV.frame.size.width + 10 * WIDTH, 5 * HEIGHT, 120 * WIDTH, 25 * HEIGHT);
    _likeCount.frame = CGRectMake(_userLabel.frame.origin.x, _userLabel.frame.size.height + 10 * HEIGHT, 100 * WIDTH, 20 * HEIGHT);
    _playCount.frame = CGRectMake(_likeCount.frame.size.width + _userImageV.frame.size.width + 15, _likeCount.frame.origin.y, 100 * WIDTH, 20 * HEIGHT);
    _feelLabel.frame = CGRectMake(5 * WIDTH, self.frame.size.height - 60 * HEIGHT, self.frame.size.width - 10 * WIDTH, 60 * HEIGHT);
    self.containerView.frame = CGRectMake(0, 60 * HEIGHT, self.frame.size.width, self.frame.size.height - 130 * HEIGHT);
    self.containerView.backgroundColor = [UIColor blackColor];
    self.progressView.frame = CGRectMake(0, self.frame.size.height - 69 * HEIGHT, self.contentView.frame.size.width, 1 * HEIGHT);
    _imageV.frame = CGRectMake(self.containerView.frame.size.width / 2 - 50 * HEIGHT, self.frame.size.height / 2 - 50 * HEIGHT, 100 * WIDTH, 100 * HEIGHT);
}
- (void)setVedioDetialModel:(ZCXVedioDetialModel *)vedioDetialModel{
    if (_vedioDetialModel != vedioDetialModel) {
        [_vedioDetialModel release];
        _vedioDetialModel = [vedioDetialModel retain];
    }
    NSDictionary *dic = (NSDictionary *)vedioDetialModel.user;
    self.strID = [dic objectForKey:@"id"];
    self.userName = [dic objectForKey:@"screen_name"];
    
    [self.userImageV sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"avatar"]]];
    self.userLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
    self.userLabel.text = [dic objectForKey:@"screen_name"];
    self.playCount.font = [UIFont systemFontOfSize:13 * WIDTH];
    self.playCount.text = [NSString stringWithFormat:@"播放次数:%@", vedioDetialModel.plays_count];
    self.likeCount.font = [UIFont systemFontOfSize:13 * WIDTH];
    self.likeCount.text = [NSString stringWithFormat:@"喜欢人数: %@", vedioDetialModel.likes_count];
    self.feelLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
    self.feelLabel.numberOfLines = 0;
    self.feelLabel.text = vedioDetialModel.caption;
    
    _player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:vedioDetialModel.video]];
    [self addNotification];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
    playerLayer.backgroundColor = [UIColor blackColor].CGColor;
    [_containerView.layer addSublayer:playerLayer];
    
    //添加手势;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pauseOrPlay:)];
    [_containerView addGestureRecognizer:tap];
    [tap release];
}
- (void)pauseOrPlay:(UITapGestureRecognizer *)tap{
    [self.delegate sendPlayer:_player];
    UIImageView *imageV = (UIImageView *)[self viewWithTag:1000];
    if (_playState == NO) {
        imageV.hidden = YES;
        [_player play];
        _playState = YES;
    }else{
        imageV.hidden = NO;
        [_player pause];
        _playState = NO;
    }
    
}
- (void)userImageViewTapAction:(UIGestureRecognizer *)tap{
    [self.delegate pushIntoUserDetial:self.strID titleName:self.userName];
}
-(void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playDidFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
-(void)playDidFinish
{
     NSLog(@"播放完成");
    [self setVedioDetialModel:_vedioDetialModel];
    UIImageView *imageV = (UIImageView *)[self viewWithTag:1000];
    imageV.hidden = NO;
    _playState = NO;
   
}
#pragma mark 给进度条添加监听事件
- (void)addObserverToProgressView{
    [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
        //当前播放的时间
        float currentTime = CMTimeGetSeconds(time);
        //获得视频的总时间
        float totalTime = CMTimeGetSeconds(_player.currentItem.duration);
        if (_player.currentItem) {
            [_progressView setProgress:currentTime / totalTime animated:YES];
        }
    }];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
